using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace UKVSTS
{
    public partial class PathSelector : Form
    {
        NodeCollection m_NodeCollection;

        public PathSelector()
        {
            InitializeComponent();
        }

        public NodeCollection NodeCollection
        {
            get { return m_NodeCollection; }
            set
            {

                ArgTest.IsNotNull(value, "NodeCollection");
                m_NodeCollection = value;

                treePath.Nodes.Clear();

                AppendChildNodes(treePath.Nodes, NodeCollection);
            }
        }

        private void AppendChildNodes(TreeNodeCollection addNodeTo, NodeCollection nodes)
        {
            foreach (Node node in nodes)
            {
                TreeNode childTreeNode = new TreeNode(node.Name);
                childTreeNode.Tag = node;

                if (node.HasWorkItemWriteRights == false)
                {
                    childTreeNode.ForeColor = SystemColors.GrayText;
                }

                addNodeTo.Add(childTreeNode);

                if (node.HasChildNodes)
                {
                    AppendChildNodes(childTreeNode.Nodes, node.ChildNodes);
                }
            }
        }

        public String GetSelectedNodePath()
        {
            String path = "";
            if (treePath.SelectedNode != null && treePath.SelectedNode.Tag != null)
            {
                Node node = treePath.SelectedNode.Tag as Node;
                if (node != null)
                {
                    path = node.Path;
                }
            }
            return path;
        }


        private void treePath_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {

        }

        private void treePath_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtPath.Text = GetSelectedNodePath();
        }

        public static Node NodeFromPath(NodeCollection rootNodes, String path)
        {
            ArgTest.IsNotNull(rootNodes, "rootNodes");
            ArgTest.IsNotNull(path, "path");

            String[] nodeNames = path.Split('\\'); // path char isn't exposed, so need to hard code :-(

            Node node = null;
            if (nodeNames.Length > 1)
            {
                try
                {
                    // 1st value in the path is the project name.
                    // so we need to use the 2nd item in the array.
                    //
                    Node rootNode = rootNodes[nodeNames[1]];

                    NodeCollection nextLevel = rootNodes;

                    for (int i = 1; i < nodeNames.Length; i++)
                    {
                        node = nextLevel [nodeNames [i]];
                        nextLevel = node.ChildNodes;
                    }
                } 
                catch (DeniedOrNotExistException) 
                {
                    node = null;
                }
            }
            return node;
        }

        private void PathSelector_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            System.Diagnostics.Process.Start("http://www.codeplex.com/UKVSTS/Wiki/View.aspx?title=Path%20Selector");
        }

    }
}